package apiTestBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created by yangxianming on 2017/9/30.
 */
public class GetVersionList implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        JSONObject returnJson = JSON.parseObject("{\"apistatus\":0,\"result\":\"\",\"errorMsg\":\"\"}");

        //查库操作
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = sqlDispatch.select("testedapi");

        //将组装返回参数
        String apistatus = (sqlReturnModel.getIsSuccess()) ? "1" : "0";
        returnJson.put("apistatus", apistatus);
        returnJson.put("errorMsg", sqlReturnModel.getFailingReason());

        //将数据库返回的数据 用适合的json格式返回
        JSONObject data = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        ArrayList<ArrayList<String>> resultArr = sqlReturnModel.getSelectReturnData();
        for (ArrayList<String> line: resultArr){
            for (String a: line){
                jsonArray.add(a);
            }
        }
        data.put("datas", jsonArray);
        returnJson.put("result", data);

        return returnJson.toString();
    }
}
